<?php

/**
 * uniBoard ACP New GeSHi
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_geshi{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_geshi( $appCore);
			
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setGeSHiTag( $appCore -> _POST( 'geshi_tag'));
			$this -> api -> setGeSHiName( $appCore -> _POST( 'geshi_name'));
			$this -> api -> setGeSHiFile( $appCore -> _POST( 'geshi_file'));
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['acp_new_geshi_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Save Api
				$this -> api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_geshi_added', htmlspecialchars( $appCore -> _POST( 'geshi_name')));
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=geshi'), $appCore -> lang['acp_geshi_saved']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_geshi'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_geshi_form_id'], $this-> appCore -> drawTextInput( 'geshi_tag', $this -> api -> geshi_tag), $this -> appCore -> lang['acp_geshi_form_id_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_geshi_form_name'], $this-> appCore -> drawTextInput( 'geshi_name', $this -> api -> geshi_name));
		
		// Use GLOB?
		if ( USE_GLOB)
		{
			// Ready array
			$geshi_files_ready = array();
			
			// Grab files
			$geshi_files = glob(GESHI_DATA_PATH . '/*' . PHP_EXT);
			
			// Got em, fix em
			$files_n = count($geshi_files);
			
			// Lengths
			$path_l = strlen( GESHI_DATA_PATH) + 1;
			$ext_l = strlen( PHP_EXT);
			
			// Iterate them
			for ( $i = 0; $i < $files_n; $i ++)
			{
				// Fix file name
				$geshi_files[$i] = substr( $geshi_files[$i], $path_l, strlen($geshi_files[$i]) - $path_l - $ext_l);
				
				// Pass to ready array
				$geshi_files_ready[$geshi_files[$i]] = $geshi_files[$i];
			}
			
			// Draw List
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_geshi_form_file'], $this-> appCore -> drawList( 'geshi_file', $geshi_files_ready, $this -> api -> geshi_file), $this -> appCore -> lang['acp_geshi_form_file_expl']);
		}
		else
		{
			// Draw text-input
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_geshi_form_file'], $this-> appCore -> drawTextInput( 'geshi_file', $this -> api -> geshi_file), $this -> appCore -> lang['acp_geshi_form_file_expl']);
		}
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['acp_new_geshi_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_new_geshi_form'], array(
			$this -> appCore -> adminLink( 'act=geshi') => $this -> appCore -> lang['acp_geshi'],
			'' => $this -> appCore -> lang['acp_new_geshi_form']
		), ob_get_clean());
			
	}

}